#include "Frm07.h"

Frm07::Frm07(HmiApp* app, STTWidget *par, const string& id, const string& desc) : STTPage(app->getRuntime(), par, id, desc) {
	hmiApp = app;
}

Frm07::~Frm07() {
}

//template<typename _Tp>
//_Tp getValue(string varname) {
//	string tmp = getRuntime()->getIVarMgr()->get(varname)->toStr();
//	istringstream iss(str);
//	_Tp num;
//	iss >> num;
//	return num;
//}

void Frm07::onInit() {
}

void Frm07::createParts() {
	setMainArg(0, 0, 1024, 600);
	setMainStyle(0, 0xffffff, 0xffffff, 0, 255, 0, 0xc0c0c0);

	/*----------------------------- MButton : wMMButton1                 -----------------------------*/
	wMMButton1 = new STTButton(p_rt, this, "wMMButton1", 1);
	wMMButton1->setMainArg(0, 0, 93, 97, false);
	wMMButton1->setImg("1", "./images/arrow_34_93X97.gray.svg", "");
	wMMButton1->setImg("0", "./images/arrow_34_93X97.svg", "");
	wMMButton1->bindOpenFormHandler(Event::CLICKED, "Frm01", PageMode::NEWWIN, NULL);

	/*----------------------------- Roller : wMRoller1                   -----------------------------*/
	wMRoller1 = new STTRoller(p_rt, this, "wMRoller1");
	wMRoller1->setMainArg(92, 0, 64, 150, 0, "[{\"id\":\"2025\",\"name\":\"2025\"}]", 0, false);
	wMRoller1->setMainStyle(0, 0xffffff, 0xffffff, 0, 255, 1, 0xc0c0c0, 0, 0x0);
	wMRoller1->setSelectedStyle(0, 0x1a2b1, 0xffffff, 0, 255);
	wMRoller1->setFontStyle(0x0, 16, "文泉驿微米黑", 0xffffff, 16, "文泉驿微米黑", 0, 0);
	wMRoller1->onEventHandler(Event::VALUE_CHANGED, this, (EHandler) &Frm07::wMRoller1_vch_cb);

	/*----------------------------- Roller : wMRoller2                   -----------------------------*/
	wMRoller2 = new STTRoller(p_rt, this, "wMRoller2");
	wMRoller2->setMainArg(155, 0, 64, 150, 0, "选项1\n选项2\n选项3", 0, false);
	wMRoller2->setMainStyle(0, 0xffffff, 0xffffff, 0, 255, 1, 0xc0c0c0, 0, 0x0);
	wMRoller2->setSelectedStyle(0, 0x1a2b1, 0xffffff, 0, 255);
	wMRoller2->setFontStyle(0x0, 16, "文泉驿微米黑", 0xffffff, 16, "文泉驿微米黑", 0, 0);
	wMRoller2->onEventHandler(Event::CLICKED, this, (EHandler) &Frm07::wMRoller2_clk_cb);
	wMRoller2->onEventHandler(Event::VALUE_CHANGED, this, (EHandler) &Frm07::wMRoller2_vch_cb);

	/*----------------------------- Roller : wMRoller3                   -----------------------------*/
	wMRoller3 = new STTRoller(p_rt, this, "wMRoller3");
	wMRoller3->setMainArg(218, 0, 64, 150, 0, "选项1\n选项2\n选项3", 0, false);
	wMRoller3->setMainStyle(0, 0xffffff, 0xffffff, 0, 255, 1, 0xc0c0c0, 0, 0x0);
	wMRoller3->setSelectedStyle(0, 0x1a2b1, 0xffffff, 0, 255);
	wMRoller3->setFontStyle(0x0, 16, "文泉驿微米黑", 0xffffff, 16, "文泉驿微米黑", 0, 0);

	/*----------------------------- Roller : wMRoller4                   -----------------------------*/
	wMRoller4 = new STTRoller(p_rt, this, "wMRoller4");
	wMRoller4->setMainArg(281, 0, 64, 150, 0, "选项1\n选项2\n选项3", 0, false);
	wMRoller4->setMainStyle(0, 0xffffff, 0xffffff, 0, 255, 1, 0xc0c0c0, 0, 0x0);
	wMRoller4->setSelectedStyle(0, 0x1a2b1, 0xffffff, 0, 255);
	wMRoller4->setFontStyle(0x0, 16, "文泉驿微米黑", 0xffffff, 16, "文泉驿微米黑", 0, 0);

	/*----------------------------- Roller : wMRoller5                   -----------------------------*/
	wMRoller5 = new STTRoller(p_rt, this, "wMRoller5");
	wMRoller5->setMainArg(344, 0, 64, 150, 0, "选项1\n选项2\n选项3", 0, false);
	wMRoller5->setMainStyle(0, 0xffffff, 0xffffff, 0, 255, 1, 0xc0c0c0, 0, 0x0);
	wMRoller5->setSelectedStyle(0, 0x1a2b1, 0xffffff, 0, 255);
	wMRoller5->setFontStyle(0x0, 16, "文泉驿微米黑", 0xffffff, 16, "文泉驿微米黑", 0, 0);

	/*----------------------------- Text : wMText1                       -----------------------------*/
	wMText1 = new STTTextArea(p_rt, this, "wMText1");
	wMText1->setMainArg(472, 1, 120, 30, false, true, TextAlign::CENTER, -1, -1, -99999999, 99999999, false);
	wMText1->setMainStyle(7, 0xffffff, 0xffffff, 0, 2, 0xc0c0c0, 4, 4, 4, 4, 255);
	wMText1->setPlaceholderText("")->setMaxLength(0)->setAcceptedChars("")->setImeMode(IME::DIGITAL);
	wMText1->setFont(0x0, 12, "文泉驿微米黑", 0);

	/*----------------------------- Label : wMLabel2                     -----------------------------*/
	wMLabel2 = new STTLabel(p_rt, this, "wMLabel2");
	wMLabel2->setMainArg(408, 0, 65, 37, "鸟蛋编号", TextAlign::LEFT, LabelLongMode::CLIP, 30);
	wMLabel2->setFonts(0x0, 12, "文泉驿微米黑", 0);
	wMLabel2->setMainStyle(0, 0xffffff, 0xffffff, GradDir::NONE, 0, 0x0, 0);
	wMLabel2->setFormatText(TextAlign::LEFT, LabelLongMode::CLIP, 30);

	/*----------------------------- Text : wMText2                       -----------------------------*/
	wMText2 = new STTTextArea(p_rt, this, "wMText2");
	wMText2->setMainArg(472, 49, 551, 30, false, true, TextAlign::CENTER, -1, -1, -99999999, 99999999, false);
	wMText2->setMainStyle(7, 0xffffff, 0xffffff, 0, 2, 0xc0c0c0, 4, 4, 4, 4, 255);
	wMText2->setPlaceholderText("")->setMaxLength(0)->setAcceptedChars("")->setImeMode(IME::T9);
	wMText2->setFont(0x0, 12, "文泉驿微米黑", 0);

	/*----------------------------- Label : wMLabel3                     -----------------------------*/
	wMLabel3 = new STTLabel(p_rt, this, "wMLabel3");
	wMLabel3->setMainArg(602, 0, 49, 37, "事项", TextAlign::RIGHT, LabelLongMode::CLIP, 30);
	wMLabel3->setFonts(0x0, 12, "文泉驿微米黑", 0);
	wMLabel3->setMainStyle(0, 0xffffff, 0xffffff, GradDir::NONE, 0, 0x0, 0);
	wMLabel3->setFormatText(TextAlign::RIGHT, LabelLongMode::CLIP, 30);

	/*----------------------------- Dropdown : wMDropdown1               -----------------------------*/
	wMDropdown1 = new STTDropdown(p_rt, this, "wMDropdown1");
	wMDropdown1->setMainArg(662, 1, 361, 30, "[{\"id\":\"0\",\"name\":\"下蛋\"},{\"id\":\"1\",\"name\":\"入器\"}]", 0, ScrollDir::BOTTOM, NULL, -1, false);
	wMDropdown1->setSymbol("↓", false);
	wMDropdown1->setFonts(0x0, 12, "文泉驿微米黑", false, 0xffffff, 12, "文泉驿微米黑", 0, 0);
	wMDropdown1->setMainStyle(8, 0xffffff, 0xffffff, 0, 2, 0xc0c0c0, 0, 0x0);
	wMDropdown1->getStyle()->p_pad->setTop(6)->setBottom(6)->setLeft(11)->setRight(11);
	wMDropdown1->setListSelectedStyle(0, 0x1a2b1, 0xffffff, 0, 0, 0x0);

	/*----------------------------- Label : wMLabel4                     -----------------------------*/
	wMLabel4 = new STTLabel(p_rt, this, "wMLabel4");
	wMLabel4->setMainArg(416, 48, 49, 37, "备注", TextAlign::RIGHT, LabelLongMode::CLIP, 30);
	wMLabel4->setFonts(0x0, 12, "文泉驿微米黑", 0);
	wMLabel4->setMainStyle(0, 0xffffff, 0xffffff, GradDir::NONE, 0, 0x0, 0);
	wMLabel4->setFormatText(TextAlign::RIGHT, LabelLongMode::CLIP, 30);

	/*----------------------------- Button : wMButton1                   -----------------------------*/
	wMButton1 = new STTButton(p_rt, this, "wMButton1", 0);
	wMButton1->setMainArg(893, 91, 129, 48, false);
	wMButton1->setPressedArg("记录", 0x0, "文泉驿微米黑", 16, 0);
	wMButton1->setPressedStyle(8, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton1->setReleasedArg("记录", 0x0, "文泉驿微米黑", 16, 0);
	wMButton1->setReleasedStyle(5, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton1->onEventHandler(Event::CLICKED, this, (EHandler) &Frm07::wMButton1_clk_cb);

	/*----------------------------- SqliteTable : wMSqliteTable1         -----------------------------*/
	wMSqliteTable1 = new STTTable(p_rt, this, "wMSqliteTable1");
	wMSqliteTable1->setMainArg(0, 273, 1025, 327, "HID,序号,47;YMD,日期,98;HMS,时间,98;TheCode,鸟蛋编号,122;TheTitle,事项,428;TheMemo,备注,192", 10, false, 6);
	wMSqliteTable1->setHeadCellStyle(0xf0f0f0, 0xffffff, 0, 1, 0xd0d0d0, 5, 5, 0, 0, 0x0, 12, "文泉驿微米黑", 0);
	wMSqliteTable1->setSelCellStyle(0x1a2b3, 0xffffff, 0, 1, 0xd0d0d0, 5, 5, 0, 0, 0x0, 12, "文泉驿微米黑", 0);
	wMSqliteTable1->setDataCellStyle(0xffffff, 0xffffff, 0, 1, 0xd0d0d0, 5, 5, 0, 0, 0x0, 12, "文泉驿微米黑", 0);
	tv_wMSqliteTable1 = new TTableView("tv_wMSqliteTable1", "HatchLog", "HID", "HID", nullptr, "", false);
	wMSqliteTable1->bindData(tv_wMSqliteTable1);

	/*----------------------------- Button : wMButton2                   -----------------------------*/
	wMButton2 = new STTButton(p_rt, this, "wMButton2", 0);
	wMButton2->setMainArg(926, 225, 98, 48, false);
	wMButton2->setPressedArg("末页", 0x0, "文泉驿微米黑", 16, 0);
	wMButton2->setPressedStyle(8, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton2->setReleasedArg("末页", 0x0, "文泉驿微米黑", 12, 0);
	wMButton2->setReleasedStyle(5, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton2->onEventHandler(Event::CLICKED, this, (EHandler) &Frm07::wMButton2_clk_cb);

	/*----------------------------- Button : wMButton4                   -----------------------------*/
	wMButton4 = new STTButton(p_rt, this, "wMButton4", 0);
	wMButton4->setMainArg(827, 225, 98, 48, false);
	wMButton4->setPressedArg("下一页", 0x0, "文泉驿微米黑", 16, 0);
	wMButton4->setPressedStyle(8, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton4->setReleasedArg("下一页", 0x0, "文泉驿微米黑", 12, 0);
	wMButton4->setReleasedStyle(5, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton4->onEventHandler(Event::CLICKED, this, (EHandler) &Frm07::wMButton4_clk_cb);

	/*----------------------------- Button : wMButton5                   -----------------------------*/
	wMButton5 = new STTButton(p_rt, this, "wMButton5", 0);
	wMButton5->setMainArg(728, 224, 98, 48, false);
	wMButton5->setPressedArg("上一页", 0x0, "文泉驿微米黑", 16, 0);
	wMButton5->setPressedStyle(8, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton5->setReleasedArg("上一页", 0x0, "文泉驿微米黑", 12, 0);
	wMButton5->setReleasedStyle(5, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton5->onEventHandler(Event::CLICKED, this, (EHandler) &Frm07::wMButton5_clk_cb);

	/*----------------------------- Button : wMButton6                   -----------------------------*/
	wMButton6 = new STTButton(p_rt, this, "wMButton6", 0);
	wMButton6->setMainArg(629, 224, 98, 48, false);
	wMButton6->setPressedArg("首页", 0x0, "文泉驿微米黑", 16, 0);
	wMButton6->setPressedStyle(8, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton6->setReleasedArg("首页", 0x0, "文泉驿微米黑", 12, 0);
	wMButton6->setReleasedStyle(5, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton6->onEventHandler(Event::CLICKED, this, (EHandler) &Frm07::wMButton6_clk_cb);

	/*----------------------------- Text : wMText3                       -----------------------------*/
	wMText3 = new STTTextArea(p_rt, this, "wMText3");
	wMText3->setMainArg(64, 231, 154, 35, false, true, TextAlign::CENTER, -1, -1, -99999999, 99999999, false);
	wMText3->setMainStyle(7, 0xffffff, 0xffffff, 0, 2, 0xc0c0c0, 4, 4, 4, 4, 255);
	wMText3->setPlaceholderText("")->setMaxLength(0)->setAcceptedChars("")->setImeMode(IME::DIGITAL);
	wMText3->setFont(0x0, 16, "文泉驿微米黑", 0);

	/*----------------------------- Label : wMLabel5                     -----------------------------*/
	wMLabel5 = new STTLabel(p_rt, this, "wMLabel5");
	wMLabel5->setMainArg(0, 230, 65, 37, "鸟蛋编号", TextAlign::LEFT, LabelLongMode::CLIP, 30);
	wMLabel5->setFonts(0x0, 12, "文泉驿微米黑", 0);
	wMLabel5->setMainStyle(0, 0xffffff, 0xffffff, GradDir::NONE, 0, 0x0, 0);
	wMLabel5->setFormatText(TextAlign::LEFT, LabelLongMode::CLIP, 30);

	/*----------------------------- Button : wMButton7                   -----------------------------*/
	wMButton7 = new STTButton(p_rt, this, "wMButton7", 0);
	wMButton7->setMainArg(229, 224, 98, 48, false);
	wMButton7->setPressedArg("查询", 0x0, "文泉驿微米黑", 16, 0);
	wMButton7->setPressedStyle(8, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton7->setReleasedArg("查询", 0x0, "文泉驿微米黑", 12, 0);
	wMButton7->setReleasedStyle(5, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton7->onEventHandler(Event::CLICKED, this, (EHandler) &Frm07::wMButton7_clk_cb);

	/*called after widget created.*/

	return; //createParts END
}


static string makeOption(int imin, int imax) {
	vector<string> vec;
	for(int i=imin;i<=imax;i++) {
		string tmp = "{\"id\":\"" + to_string(i) + "\",\"name\":\"" + to_string(i) + "\"}";
		vec.push_back(tmp);
	}
	string ret = "[" + StringUtil::join(vec, ",") + "]";
	return ret;
}

static string makeYearOption() {
	int year = DateUtil::getDate("Y");
	return makeOption(year-1,year+1);
}

static string makeMonthOption() {
	return makeOption(1,12);
}

static string makeDayOption(int year, int month) {
	int maxday = 31;
	if(month == 4 || month == 6 || month == 9 || month == 11) maxday = 30;
	else if(month == 2) {
		if(year % 400 == 0 || (year%4==0 && year%100!=0)) {
			maxday = 29;
		} else {
			maxday = 28;
		}
	}
	return makeOption(1,maxday);
}

static string makeHourOption() {
	return makeOption(0,23);
}

static string makeSecondOption() {
	return makeOption(0,59);
}

static void createHatchLog() {
	string sql = R"( CREATE TABLE IF NOT EXISTS HatchLog(
HID INTEGER PRIMARY KEY AUTOINCREMENT
, YMD INTEGER
, HMS INTEGER
, TheCode TEXT
, TheTitle TEXT
, TheMemo TEXt
); )";
	DBMgr::getdb()->execute(sql);
}

static void insertHatchLog(Frm07* frm) {
	int y = StringUtil::safeStoi(frm->wMRoller1->getSelectedText());
	int m = StringUtil::safeStoi(frm->wMRoller2->getSelectedText());
	int d = StringUtil::safeStoi(frm->wMRoller3->getSelectedText());
	int hh = StringUtil::safeStoi(frm->wMRoller4->getSelectedText());
	int mm = StringUtil::safeStoi(frm->wMRoller5->getSelectedText());
	string code = frm->wMText1->getText();
	string title = frm->wMDropdown1->getSelectedText();
	string memo = frm->wMText2->getText();
	int ymd = y*10000+m*100+d;
	int hms = hh*100+mm;
	string sql = R"( 
INSERT INTO HatchLog (YMD,HMS,TheCode,TheTitle,TheMemo) VALUES ({0},{1},'{2}','{3}','{4}')
)";
	sql = StringUtil::replace(sql, "{0}", to_string(ymd));
	sql = StringUtil::replace(sql, "{1}", to_string(hms));
	sql = StringUtil::replace(sql, "{2}", code);
	sql = StringUtil::replace(sql, "{3}", title);
	sql = StringUtil::replace(sql, "{4}", memo);
	DBMgr::getdb()->execute(sql);
}
static void queryHatchLog(Frm07* frm) {
	string code = frm->wMText3->getText();
	string sql = "SELECT * FROM HatchLog WHERE TheCode='{0}'";
	if(code!="") {
		sql = StringUtil::replace(sql, "{0}", code);
	} else sql = "SELECT * FROM HatchLog";
	sql = "( " + sql + " ) T1";
	frm->tv_wMSqliteTable1->refine(sql);
	frm->tv_wMSqliteTable1->drawData(true);
}


void Frm07::onLoad() {
	int year = DateUtil::getDate("Y");
	int month = DateUtil::getDate("M");
	wMRoller1->setOptions(makeYearOption(), 1);
	wMRoller2->setOptions(makeMonthOption(), DateUtil::getDate("M")-1);
	wMRoller3->setOptions(makeDayOption(year,month), DateUtil::getDate("D")-1);
	wMRoller4->setOptions(makeHourOption(), DateUtil::getDate("hh"));
	wMRoller5->setOptions(makeSecondOption(), DateUtil::getDate("mm"));
	tv_wMSqliteTable1->enDesc(true);
	createHatchLog();
}

bool Frm07::onClosing() {
	return true;
}

void Frm07::onDispose() {

}

void Frm07::wMRoller2_vch_cb(uint16_t code, LvEvent e) {

}

void Frm07::wMRoller2_clk_cb(uint16_t code, LvEvent e) {
	int i = wMRoller3->getSelectedIndex();
	int year = StringUtil::safeStoi(wMRoller1->getSelectedValue());
	int month = wMRoller2->getSelectedIndex() + 1;
	wMRoller3->setOptions(makeDayOption(year, month), i);
}

void Frm07::wMRoller1_vch_cb(uint16_t code, LvEvent e) {
	int i = wMRoller3->getSelectedIndex();
	int year = StringUtil::safeStoi(wMRoller1->getSelectedValue());
	int month = wMRoller2->getSelectedIndex() + 1;
	wMRoller3->setOptions(makeDayOption(year, month), i);
}
void Frm07::wMButton1_clk_cb(uint16_t code, LvEvent e) {
	/*wMButton1（记录）的点击事件*/
	insertHatchLog(this);
	queryHatchLog(this);
}

void Frm07::wMButton7_clk_cb(uint16_t code, LvEvent e) {
	/*wMButton7（查询）的点击事件*/
	queryHatchLog(this);
}

void Frm07::wMButton6_clk_cb(uint16_t code, LvEvent e) {
	/*wMButton6（首页）的点击事件*/
	this->wMSqliteTable1->navFirst();
}

void Frm07::wMButton5_clk_cb(uint16_t code, LvEvent e) {
	/*wMButton5（上一页）的点击事件*/
	this->wMSqliteTable1->navBack();
}

void Frm07::wMButton4_clk_cb(uint16_t code, LvEvent e) {
	/*wMButton4（下一页）的点击事件*/
	this->wMSqliteTable1->navNext();
}

void Frm07::wMButton2_clk_cb(uint16_t code, LvEvent e) {
	/*wMButton2（末页）的点击事件*/
	this->wMSqliteTable1->navLast();
}
